package com.guigu.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guigu.qingcheng.mapper.TbAdminMapper;
import com.guigu.qingcheng.pojo.TbAdmin;
import com.guigu.qingcheng.service.TbAdminService;
import com.guigu.qingcheng.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
*
* @author Jiang
* @date 2020/08/05
*/
@Service
public class TbAdminServiceImpl implements TbAdminService {
    @Autowired(required = false)
    private TbAdminMapper tbAdminMapper;



    @Override
    public List<TbAdmin> queryAll() {
        List<TbAdmin> tbAdmin = tbAdminMapper.selectAll();
            return tbAdmin;
    }

    @Override
    public PageUtil<TbAdmin> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbAdmin> pages = (Page<TbAdmin>) tbAdminMapper.selectAll();
        PageUtil<TbAdmin> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbAdmin> pageQueryByAll(Integer pageNum, Integer pageSize, TbAdmin tbAdmin) {

        Example example=new Example(TbAdmin.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO 待实现 重新实现判断参数

        //if(!StringUtils.isEmpty(brand.getId())){
        //    criteria.andEqualTo("id",brand.getId());
        //}
        //if(!StringUtils.isEmpty(brand.getName())){
        //   criteria.andLike("name","%"+brand.getName()+"%");
        //}
        PageHelper.startPage(pageNum,pageSize);
        Page<TbAdmin> pages = (Page<TbAdmin>) tbAdminMapper.selectByExample(example);
        PageUtil<TbAdmin> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbAdmin(TbAdmin tbAdmin) {
        int insert = tbAdminMapper.insert(tbAdmin);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbAdmin(TbAdmin tbAdmin) {
        int i = tbAdminMapper.updateByPrimaryKeySelective(tbAdmin);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbAdminMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbAdmin findById(Integer id) {
        TbAdmin tbAdmin = tbAdminMapper.selectByPrimaryKey(id);
        return tbAdmin;
    }
}
